package fft;
import processing.core.PApplet;
import ddf.minim.*;
import java.io.File;
import java.io.FileFilter;
import java.math.MathContext.*;
import javax.swing.JFileChooser;
import org.gwoptics.graphics.graph2D.Graph2D;
import org.gwoptics.graphics.graph2D.traces.Line2DTrace;
import processing.core.PFont;
import processing.core.PGraphics;
import processing.core.PVector;
import javax.swing.*;
import com.sun.java.swing.plaf.motif.MotifLookAndFeel;
/**
 *  @brief The main core of the program
 *  @author Frequent Flyers
 */

public class FFTProgram extends PApplet
{   
    static StatAnalysis m_corr;
    static FFTAnalysis m_ana;
    static FFTAnalysis m_ana2;
    PFont f; 
    
    /*Graph*/
    Graph2D g;
    LineGraph sg1;
    LineGraph sg2;
    LineGraph sg3;
    
    float lineX = 70;
    final static JFileChooser fchooser =  new JFileChooser();
    
    /*Set everything up and Run the FFT*/
    @Override
    public void setup() 
    {
        setupFileChooser();
        String file1 = browse();
        String file2 = browse();
        size (1300, 725);     
        textFont(createFont("SanSerif", 12));
        smooth();
        //noLoop();
        
        m_ana = new FFTAnalysis(this);       
        m_ana.minim = new Minim(this);
       
        m_ana2 = new FFTAnalysis(this);
        m_ana2.minim = new Minim(this);
        
        m_ana.Setup(file1, "default");
        m_ana.RunFFT();

        m_ana2.Setup(file2, "lossy");
        m_ana2.RunFFT();
       
        if (m_ana.DONE == true && m_ana2.DONE == true) {
            System.out.println("Done.");
            m_corr = new StatAnalysis(m_ana.frequency,m_ana.signal1, m_ana2.signal1);
            m_corr.MeanSquaredAvg();
            //m_corr.crossCorrelate();
        }
        
        /*Add a graph and populate it with data*/
        g = new Graph2D(this, 1200,600,false);
        
        /*Defining the main properties of the X and Y-Axis */
        g.setYAxisMin(-1);
        g.setYAxisMax(300);
        g.setXAxisMin(0);
        g.setXAxisMax(22044);
        g.setXAxisLabel("Frequency");
        g.setYAxisLabel("Amplitude");
        g.setXAxisTickSpacing((float)2000.0);
        g.setYAxisTickSpacing((float)50.0);
        
        /*Position */
        g.position.x = 70;
        g.position.y = 30;
        
        /*Make the graphs, assign the data */
        sg1 = new LineGraph();
        sg1.bindData(m_ana.frequency, m_ana.signal1, new float[]{255,0,0,255});
        
        sg2 = new LineGraph();
        sg2.bindData(m_ana2.frequency, m_ana2.signal1,new float[]{0,0,255,255});  
        
        sg3 = new LineGraph();
        sg3.bindData(m_ana.frequency,m_corr.dif,new float[]{0,255,0,255});
                
        /*Adding the trace to the graph */
        g.addTrace(sg1);
        g.addTrace(sg2);
        g.addTrace(sg3);
        
    }
    
    /**Draw the FFT graph */
    @Override
    public void draw() 
    {   
        background(255);       
        f = loadFont("ArialMT-48.vlw");
        textFont(f, 24);                 
        fill(0);                        
        text("FFT Spectrum Analyzer", 500, 70);  
        
        textFont(f, 16);                 
        fill(0);                         
        text("Lossless(WAV) - Red", 500, 100);  
        text("Lossy(MP3) - Blue", 500, 120);
        text("MSE - Green", 500, 140);
        text("1,2,3 - Hide lines", 500, 160);
        text("q,w,e - Show lines", 500, 180);
        fill(255);
        stroke(255);
        line(lineX, 30,lineX, 725);
        g.draw();

    }

    /**Stop minim on close along with any other resources */
    @Override
    public void stop() 
    {
        m_ana.Cleanup();
        m_ana2.Cleanup();
        super.stop();
    }
    
    /**Read user input */
    @Override
    public void keyPressed() {
        if (key == '1') 
        {
           sg1.setWeight(0);
           sg1.generate();
        } 
        else if (key == '2') 
        {
            sg2.setWeight(0);
            sg2.generate();
        } 
        else if (key == '3') 
        {
            sg3.setWeight(0);
            sg3.generate();
        }
        else if (key == 'q')
        {
            sg1.setWeight(1);
            sg1.generate();    
        }
        else if(key == 'w')
        {
            sg2.setWeight(1);
            sg2.generate();  
        }
        else if(key == 'e')
        {
            sg3.setWeight(1);
            sg3.generate();
        }          
    }
    
    /**Main function, create processing window here */
    public static void main(String args[]) 
    {      
       PApplet.main(new String[]{/*"--present",*/ "fft.FFTProgram"});       
    }
    
    /**Setup the file browser*/
    public void setupFileChooser()
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        
        /*Add a filter for directories and .wav only*/
        extensionFilter myFilter = new extensionFilter();
        fchooser.addChoosableFileFilter(myFilter);
    }
    
    /**Browse for files */
    public String browse()
    {
        String filename = "";
        int returnVal = fchooser.showOpenDialog(this);
        if(returnVal == JFileChooser.APPROVE_OPTION)
        {
            File file = fchooser.getSelectedFile();
            filename = file.getPath();
        }
        else
        {
            System.exit(0);
        }
        return filename;
    }
        
}
